﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class FND_WTS : RibbonForm
    {
        public FND_WTS()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public string DocumentNumber { get; set; }

        void BindGridByDocument(string _DocNumber)
        {
            WTManager oManager = new WTManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            
            gridControl1.DataSource = oManager.BrowseTransferByDocNum(_DocNumber);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByReference(string _DocNumber)
        {
            WTManager oManager = new WTManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();


            gridControl1.DataSource = oManager.BrowseTransferByReference(_DocNumber);
            gridControl1.Refresh();

            oManager.Close();
        }


        void BindGridBySourceWH(string _SourceWH)
        {
            WTManager oManager = new WTManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();


            gridControl1.DataSource = oManager.BrowseTransferBySourceWH(_SourceWH);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByDestWH(string _DestWH)
        {
            WTManager oManager = new WTManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();


            gridControl1.DataSource = oManager.BrowseTransferByDestWH(_DestWH);
            gridControl1.Refresh();

            oManager.Close();
        }

        private void txtBPCode_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (cboSearch.SelectedIndex == 0)
            {
                BindGridByDocument(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 1)
            {
                BindGridByReference(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 2)
            {
                BindGridBySourceWH(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 3)
            {
                BindGridByDestWH(txtSearch.Text);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {

            int iRowIndex = gridView1.FocusedRowHandle;

            this.DocumentNumber = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["DocNum"]).ToString();

            this.Close();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void FND_SO_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindGridByDocument(txtSearch.Text);
            }
            if (e.KeyCode == Keys.Tab)
            {
                BindGridByDocument(txtSearch.Text);
            }
        }
    }
}
